package com.cloud.rou.im.router;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

/**
 * <p></p>
 *
 * @author GaoShuangWen
 * @since 2019-11-14 23:37
 */
public class Server {
    private static ZooKeeper zk;
    private static final String CONNECT_STRING = "47.105.105.217:8088";
    private static final int SESSION_TIMEOUT = 5000;
    private static final String PARENT = "/im/instance";

    public static void main(String[] args) throws Exception {
        zk = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                String path = event.getPath();
                Event.EventType type = event.getType();
                Event.KeeperState state = event.getState();
                System.out.println(path + "\t" + type + "\t" + state);

                // 循环监听
                try {
                    zk.getChildren(PARENT, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        // 添加监听
        zk.getChildren(PARENT, true);

        // 模拟服务器一直运行
        Thread.sleep(Long.MAX_VALUE);
    }
}
